import onloadHook from "../utils/onloadHook";
import request from "../utils/request";
export function blankScreen() {
    let wrapperElements = ['html', 'body'],
        emptyPoints = 0;

    function getSelector(element){
        if(element.id){
            return `#${element.id}`
        } else if(element.className){
            return `.${element.className}`
        } else{
            return element.nodeName.toLowerCase();
        }
    }

    function isWrapper(element) {
        let selector = getSelector(element);
        if(wrapperElements.includes(selector)){
            emptyPoints++;
        }
    }

    onloadHook(function(){
        for (let i = 1; i <= 9; i++) {
            let xElements = document.elementsFromPoint(window.innerWidth * i / 10, window.innerHeight / 2);
            let yElements = document.elementsFromPoint(window.innerWidth / 2, window.innerHeight * i / 10);
            isWrapper(xElements[0]);
            isWrapper(yElements[0]);
        }
        if(emptyPoints > 16){
            let centerPoint = document.elementsFromPoint(window.innerWidth / 2, window.innerHeight / 2)[0];
            request.send({
                kind: 'stability',
                type: 'blankScreen',
                emptyPoints,
                screen: `${window.screen.width}x${window.screen.height}`,
                viewPort: `${window.innerWidth}x${window.innerHeight}`,
                selector: getSelector(centerPoint)
            })
        }
    })
    
}